﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication9.Models;

namespace MvcApplication9.Controllers
{ 
    public class CabinsController : Controller
    {
        private MainModelContainer db = new MainModelContainer();
        static int? Id;
        //
        // GET: /Cabins/

        public ViewResult Index(int? ship_id)
        {
            //var cabins = db.Cabins.Include("Ship");
            List<Cabins> cabins = null;
            if (ship_id != null)
            {
                Id = ship_id;
            }
            cabins = (from i in db.Cabins where i.Ship.Id == Id select i).ToList();
            return View(cabins);
        }

        //
        // GET: /Cabins/Details/5

        public ViewResult Details(int id)
        {
            Cabins cabins = db.Cabins.Single(c => c.Id == id);
            return View(cabins);
        }

        //
        // GET: /Cabins/Create

        public ActionResult Create()
        {
            return RedirectToAction("AddCabins", "Ships", new { id = Id });
        } 

        //
        // POST: /Cabins/Create

        [HttpPost]
        public ActionResult Create(Cabins cabins)
        {
            if (ModelState.IsValid)
            {
                db.Cabins.AddObject(cabins);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ShipId = new SelectList(db.Ships, "Id", "Name", cabins.ShipId);
            return View(cabins);
        }
        
        //
        // GET: /Cabins/Edit/5
 
        public ActionResult Edit(int id)
        {
            Cabins cabins = db.Cabins.Single(c => c.Id == id);
            return View(cabins);
        }

        //
        // POST: /Cabins/Edit/5

        [HttpPost]
        public ActionResult Edit(Cabins cabins)
        {
            if (ModelState.IsValid)
            {
                db.Cabins.Attach(cabins);
                db.ObjectStateManager.ChangeObjectState(cabins, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(cabins);
        }

        //
        // GET: /Cabins/Delete/5
 
        public ActionResult Delete(int id)
        {
            Cabins cabins = db.Cabins.Single(c => c.Id == id);
            return View(cabins);
        }

        //
        // POST: /Cabins/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Cabins cabins = db.Cabins.Single(c => c.Id == id);
            db.Cabins.DeleteObject(cabins);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}